package com.fadadianqi.logistics.admin.net.mina;

import com.fadadianqi.logistics.admin.net.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;

@Service("ioHandler")
public class FadaIOHandler implements IoHandler {

    private static final Log LOG = LogFactory.getLog(FadaIOHandler.class);

    private Connection connection = null;

    @Override
    public void exceptionCaught(IoSession ioSession, Throwable arg1)
            throws Exception {
        arg1.printStackTrace();
        LOG.error("#exceptionCaught#:" + arg1.toString());
        String id = ioSession.getAttribute(SessionAttrName.ID).toString();
        Session session = SessionManager.getSession(id);
        session.close();
        SessionManager.delSession(id);
    }

    @Override
    public void messageReceived(IoSession ioSession, Object message) throws Exception {
        String id = ioSession.getAttribute(SessionAttrName.ID).toString();
        LOG.debug("#messageReceived#: sessionId:" + id + "," + message.getClass().toString());
        Packet packet = (Packet) message;
        packet.setSession(SessionManager.getSession(id));
        ChannelHandler<?> channelHandler = ChannelHandlerFactory.getNewHandler(packet);
        if (channelHandler == null) {
            LOG.error("#messageReceived#, not found ChannelHandler for " + packet.getClass().getSimpleName());
        } else {
            Channel<Packet> channel = new Channel<Packet>((ChannelHandler<Packet>) channelHandler);
            channel.add(packet);
        }
        //
    }

    @Override
    public void messageSent(IoSession ioSession, Object message) throws Exception {
        String id = ioSession.getAttribute(SessionAttrName.ID).toString();
        LOG.debug("#messageSent#: sessionId:" + id + "," + message.getClass().getCanonicalName());
    }

    @Override
    public void sessionClosed(IoSession ioSession) throws Exception {
        String id = ioSession.getAttribute(SessionAttrName.ID).toString();
        LOG.debug("#sessionClosed#: sessionId:" + id);
        Session session = SessionManager.getSession(id);
        if (session != null) {
            session.close();
        }
    }

    @Override
    public void sessionCreated(IoSession ioSession) throws Exception {
        String clientIP = ((InetSocketAddress) ioSession.getRemoteAddress()).getAddress().getHostAddress();
        ioSession.setAttribute("ip", clientIP);
        connection = new NIOConnection(ioSession);
        ioSession.setAttribute(SessionAttrName.CONNECTION, connection);
        SessionManager.addSession(new ClientSession(ioSession));
        String id = ioSession.getAttribute(SessionAttrName.ID).toString();
        LOG.debug("#sessionCreated#: sessionId:" + id);
    }

    @Override
    public void sessionIdle(IoSession ioSession, IdleStatus idle) throws Exception {
        String id = ioSession.getAttribute(SessionAttrName.ID).toString();
        LOG.debug("#sessionIdle#: sessionId:" + id);

    }

    @Override
    public void sessionOpened(IoSession ioSession) throws Exception {
        String id = ioSession.getAttribute(SessionAttrName.ID).toString();
        LOG.debug("#sessionOpened#: sessionId:" + id);
    }

} 
